/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.scheduling;

import com.asiainfo.redis.entity.BusiDataRule;
import com.asiainfo.redis.entity.BusiType;
import com.asiainfo.redis.entity.Cluster;
import com.asiainfo.redis.entity.ClusterBusiTypeRef;
import com.asiainfo.redis.repository.BusiTypeRepository;
import com.asiainfo.redis.repository.ClusterRepository;
import com.asiainfo.redis.service.BusiConfigService;
import com.asiainfo.redis.service.ScheduleService;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @version v 1.0 on 2016/8/11 15:30
 * @auther william.xu
 */
public class RefreshAllBusiTypeDataJob extends BaseJob {

    protected static final Logger logger = LoggerFactory.getLogger(RefreshAllBusiTypeDataJob.class);

    public void executeInternal(JobExecutionContext context) throws JobExecutionException {

        JobDataMap jobDataMap = context.getMergedJobDataMap();
        Long id = (Long)jobDataMap.get("ID");
        ScheduleService scheduleService = applicationContext.getBean(ScheduleService.class);
        ClusterRepository clusterRepository = applicationContext.getBean(ClusterRepository.class);
        BusiTypeRepository busiTypeRepository = applicationContext.getBean(BusiTypeRepository.class);
        ClusterBusiTypeRef clusterBusiTypeRef = scheduleService.getClusterBusiTypeRef(id);

        Cluster cluster = clusterRepository.findOne(clusterBusiTypeRef.getClusterId());
        Assert.notNull(cluster);
        BusiType busiType = busiTypeRepository.findOne(clusterBusiTypeRef.getBusiTypeId());
        Assert.notNull(busiType);

        logger.info("正在全量刷新 "+cluster.getClusterName()+" 下的 "+busiType.getBusiName() +"("+busiType.getBusiCode()+")"+" 的数据...");

        try {
            scheduleService.checkForRefreshData(clusterBusiTypeRef);
            scheduleService.refreshAllData(clusterBusiTypeRef);
        }catch (Exception e){
            logger.error("刷新全量数据出现错误：", e);
        }


    }


}
